package com.joey.user.service.imp;


import com.joey.common.exception.AccountNotFoundException;
import com.joey.common.exception.PasswordErrorException;
import com.joey.user.entity.Employee;
import com.joey.user.entity.dto.EmployeeLoginDTO;
import com.joey.user.mapper.EmployeeMapper;
import com.joey.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * @author: Joey
 * @Description:
 * @date:2024/6/22 17:00
 */
@Service
public class UserSerivceImp implements UserService {

    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    public Employee login(EmployeeLoginDTO e) {
        String username = e.getUsername();
        String password = e.getPassword();

        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);

        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException("MessageConstant.ACCOUNT_NOT_FOUND");
        }
        //密码比对
        password =  DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())) {
            throw new PasswordErrorException("MessageConstant.PASSWORD_ERROR");
        }

        return employee;
    }
}
